package com.hhit.collaborative_learning.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.hhit.collaborative_learning.dao.TeamUserDao;
import com.mysql.jdbc.StringUtils;

public class TeamUserDaoImpl implements TeamUserDao{

	@Override
	public ResultSet query_user(Connection con, int start, int end, String find,
			String order, String sort) {
		// TODO Auto-generated method stub
		String sql = "SELECT tu.*,u.userNickName,u.userIcon from collaborative_learning.team_user as tu left join collaborative_learning.user as u on u.userId=tu.userId ";
		if (!StringUtils.isNullOrEmpty(find)) {
			sql += " where " + find;
		}
		if (!StringUtils.isNullOrEmpty(sort)) {
			sql += " order by " + sort;
			if (!StringUtils.isNullOrEmpty(order)) {
				sql += " " + order;
			}
		}
		if (end != 0 && start < end) {
			sql += " limit " + start + "," + end;
		}
		PreparedStatement prs;
		try {
			prs = con.prepareStatement(sql);
			return prs.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("非法请求：" + sql);
			return null;
		}
	}

	@Override
	public int insertArray(Connection con, Map<Integer,String> map, String teamId) {
		// TODO Auto-generated method stub
		String sql="INSERT INTO `collaborative_learning`.`team_user`(`userId`,`teamId`,`CreatedOn`,`desc`)VALUES(?,?,?,?)";
		System.out.println("map size " +map.size());
		try {
			con.setAutoCommit(false);
			PreparedStatement prest =con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			Iterator<Entry<Integer, String>> it = map.entrySet().iterator();
			while(it.hasNext()){
				Entry<Integer, String> entry=it.next();
				System.out.println("entry.getKey() "+entry.getKey()+" entry.getValue() "+entry.getValue());
				 prest.setInt(1, entry.getKey());
				 prest.setInt(2, Integer.valueOf(teamId));
				 prest.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
				 prest.setString(4,entry.getValue());
				 prest.addBatch();
			}
			int re[]=prest.executeBatch();
			con.commit();   
			con.setAutoCommit(true);
			int sucNum=0;
			for(int i:re){
				sucNum+=i;
			}
			return sucNum;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("非法请求  "+sql);
			return 0;
		}
	}

	@Override
	public int deletArray(Connection con, String find) {
		// TODO Auto-generated method stub
		String sql="DELETE FROM `collaborative_learning`.`team_user` WHERE "+find;
		try {
			PreparedStatement prs = con.prepareStatement(sql);
			return prs.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			System.out.println("非法sql语句  "+sql);
			return 0;
		} 
	}

}
